package com.ysm.smfu.fragment;

import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.WriterException;
import com.ysm.smfu.R;
import com.ysm.smfu.activity.PaysetMoneyDetailActivity;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.utils.MyImageBitmapUtil;
import com.ysm.smfu.utils.ToastUtils;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.welcome.SharedConfig;
import com.ysm.smfu.zxing.encoding.EncodingHandler;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/10/14.
 */
public class PaysetMoneyFragment extends MyBaseFragment {


    @BindView(R.id.consumer_qr)
    ImageView consumerQr;
    @BindView(R.id.saveToLoc)
    AutoLinearLayout saveToLoc;
    @BindView(R.id.consumer_setcount)
    TextView consumerSetcount;
    @BindView(R.id.activity_main_top_bar)
    AutoRelativeLayout activityMainTopBar;
    @BindView(R.id.clearQr)
    TextView clearQr;
    private Bitmap bitmap;
    private SharedPreferences sp;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = UIUtils.inflate(R.layout.fragment_consumer_qr);
        ButterKnife.bind(this, view);

        activityMainTopBar.setVisibility(View.GONE);
        consumerQr = (ImageView) view.findViewById(R.id.consumer_qr);
        sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
//        获取本地保存的商户名
        final String name = sp.getString(SpKey.NAME, "");
        final String businessId = new SharedConfig(UIUtils.getContext(), name).getPrivateSp().getString(SpKey.BUSSINESSID, "");
        if (TextUtils.isEmpty(businessId)) {
            ToastUtils.showToast("无法获取商户信息,请重新登录");
            return view;
        }
        createQr(businessId, null);
        return view;
    }

    /**
     * 创建二维码
     *
     * @param bitmapLoc
     */
    private void createQr(String businessId, Bitmap bitmapLoc) {
        try {
            bitmap = EncodingHandler.createQRCode(SpKey.QRPREPAY + businessId, 400, 400, bitmapLoc);
            consumerQr.setImageBitmap(bitmap);
        } catch (WriterException e) {
            // 生成二维码失败
            AlertDialog dialog = new AlertDialog.Builder(getActivity())
                    .setTitle("提示")
                    .setMessage("账单二维码生成失败,请重试")
                    .setNegativeButton("确定", null)
                    .create();
            dialog.show();
        }
    }

    @OnClick({R.id.saveToLoc, R.id.consumer_setcount})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.saveToLoc:
                saveToLoc();
                break;
            case R.id.consumer_setcount:
                UIUtils.startActivity(PaysetMoneyDetailActivity.class);
                break;
            case R.id.back:
                getActivity().finish();
                break;
        }
    }
    /**
     * 保存到本地
     */
    private void saveToLoc() {
        MyImageBitmapUtil.saveQrToLoc(getActivity(), bitmap);
    }
}
